package com.example.demo.model;

import com.baidu.bas.annotation.Erupt;
import com.baidu.bas.annotation.EruptField;
import com.baidu.bas.annotation.config.QueryExpression;
import com.baidu.bas.annotation.config.SkipSerialize;
import com.baidu.bas.annotation.model.Tenancy;
import com.baidu.bas.annotation.sub_field.Edit;
import com.baidu.bas.annotation.sub_field.EditType;
import com.baidu.bas.annotation.sub_field.Searchable;
import com.baidu.bas.annotation.sub_field.View;
import com.baidu.bas.annotation.sub_field.sub_edit.Search;
import com.baidu.bas.upms.model.EruptUser;
import com.baidu.bas.upms.model.base.HyperModel;
import com.deepzero.mmma.api.campaigns.vo.Creatives;
import com.deepzero.mmma.api.campaigns.vo.Crowd;
import com.deepzero.mmma.api.campaigns.vo.Strategy;
import com.deepzero.mmma.api.enums.CampaignStatus;
import com.deepzero.mmma.api.vo.Goal;
import com.deepzero.mmma.api.vo.Trigger;
import com.example.demo.model.custom.SimpleTaskDataProxy;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.vladmihalcea.hibernate.type.json.JsonStringType;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.collections4.CollectionUtils;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;

import javax.persistence.*;

/**
 * @author liuzhenchuan@foxmail.com
 * @date 2020/12/21
 */
@Erupt(name = "营销任务",dataProxy={SimpleTaskDataProxy.class},orderBy = "id desc")
@Table(name = "ma_simple_task")
@Entity
@TypeDef(name = "json", typeClass = JsonStringType.class)
@Getter
@Setter
@DynamicUpdate
public class SimpleTask extends HyperModel {
    /**
     * 任务名称
     */
    @EruptField(
            views = @View(title = "名称"),
            edit = @Edit(title = "名称")
    )
    private String name;
    /**
     * 目标
     */
    @Type(type = "json")
    @Column(name = "goal", columnDefinition = "json")
    @EruptField(
            views = @View(title = "目标"),
            edit = @Edit(title = "目标",type = EditType.HIDDEN)
    )
    private Goal goal;
    /**
     * 触发类型
     */
    @Type(type = "json")
    @Column(name = "triggar", columnDefinition = "json")
    @EruptField(
            views = @View(title = "触发类型"),
            edit = @Edit(title = "触发类型",type = EditType.HIDDEN)
    )
    private Trigger trigger;

    @Column(name = "trigger_type")
    @EruptField(
            views = @View(title = "触发类型"),
            edit = @Edit(title = "触发类型",type = EditType.HIDDEN),
            searchable = @Searchable(omitBlank = true,permitOperation = {QueryExpression.in,QueryExpression.EQ})
    )
    private String triggerType;

    public String getTriggerType(){
        if(trigger==null){
            return null;
        }
        return trigger.getType();
    }

    /**
     * 人群
     */
    @Type(type = "json")
    @Column(name = "crowd", columnDefinition = "json")
    @EruptField(
            views = @View(title = "人群"),
            edit = @Edit(title = "人群",type = EditType.HIDDEN)
    )
    private Crowd crowd;

    @Column(name = "crowd_type")
    @EruptField(
            views = @View(title = "人群类型"),
            edit = @Edit(title = "人群类型",type = EditType.HIDDEN),
            searchable = @Searchable(omitBlank = true,permitOperation = {QueryExpression.in,QueryExpression.EQ})
    )
    private String crowdType;

    public String getCrowdType(){
        if(this.crowd==null){
            return null;
        }
        return crowd.getType();
    }

    /**
     * 触达配置
     */
    @Type(type = "json")
    @Column(name = "strategy", columnDefinition = "json")
    @EruptField(
            views = @View(title = "触达配置"),
            edit = @Edit(title = "触达配置",type = EditType.HIDDEN)
    )
    Strategy strategy;

    @Type(type = "json")
    @Column(name = "creatives", columnDefinition = "json")
    @EruptField(
            views = @View(title = "营销内容"),
            edit = @Edit(title = "营销内容",type = EditType.HIDDEN)
    )
    private Creatives creatives;

    @Column(name = "creative_type")
    @EruptField(
            views = @View(title = "物料类型"),
            edit = @Edit(title = "物料类型"),
            searchable = @Searchable(omitBlank = true,permitOperation = {QueryExpression.in,QueryExpression.EQ})
    )
    private String creativeType;

    public String getCreativeType(){
        if(creatives!=null && !CollectionUtils.isEmpty(creatives.getData())){
            return creatives.getData().get(0).getCreative().getType();
        }
        return null;
    }

    @EruptField(
            views = @View(title = "状态"),
            edit = @Edit(title = "状态"),
            searchable = @Searchable(omitBlank = true,permitOperation = {QueryExpression.in,QueryExpression.EQ})
    )
    @Enumerated(value = EnumType.STRING)
    CampaignStatus status;

    @ManyToOne
    @EruptField(
            views ={
                    @View(title = "ID", column = "id"),
                    @View(title = "通道", column = "name")
            },
            edit = @Edit(title = "通道", type = EditType.REFERENCE_TABLE, search = @Search)
    )
    private Channel channel;


    @ManyToOne
    @EruptField(
            views = {
                    @View(title = "ID", column = "id"),
                    @View(title = "账号", column = "name")
            },
            edit = @Edit(title = "账号", type = EditType.REFERENCE_TABLE, search = @Search),
            searchable = @Searchable(omitBlank = true,permitOperation = {QueryExpression.in,QueryExpression.EQ})
    )
    private Account account;

    @ManyToOne
    @EruptField(
            views = {
                    @View(title = "ID", column = "id"),
                    @View(title = "分组", column = "name")
            },
            edit = @Edit(title = "分组", type = EditType.REFERENCE_TABLE, search = @Search)
    )
    private Group group;

    @EruptField(
            views = {
                    @View(title = "累计进入流程人数", column = "inUserNum"),
                    @View(title = "发送成功人数", column = "successNum"),
                    @View(title = "达标人数", column = "goalUserNum"),
                    @View(title = "发送失败人数", column = "failedNum"),
            },
            edit = @Edit(title = "任务总览", type = EditType.COMBINE, search = @Search,readOnly = true)
    )
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name="overviewReportId",referencedColumnName="id")
    @JsonIgnoreProperties(value="simpleTask")
    private TaskOverviewReport overviewReport;

    @ManyToOne(fetch = FetchType.LAZY)
    @EruptField(
            views = @View(title = "创建人", column = "name"),
            edit = @Edit(title = "创建人", type = EditType.REFERENCE_TABLE, search = @Search),
            searchable = @Searchable(omitBlank = true,permitOperation = {QueryExpression.in,QueryExpression.EQ})
    )
    private EruptUser createUser;

    @Column(name = "tenancy")
    @EruptField(
            views = @View(title = "租户"),
            edit = @Edit(title = "租户"),
            searchable = @Searchable(omitBlank = true,permitOperation = {QueryExpression.in,QueryExpression.EQ})
    )
    @Tenancy
    private String tenancy;

}
